namespace Tools
{
    public class SimpleSingletonProvider<T>
        where T : class, new()
    {
        private static T _inst;

        private static readonly object _lockHelper = new object();

        public static T inst
        {
            get
            {
                if (_inst == null)
                {
                    lock (_lockHelper)
                    {
                        if (_inst == null)
                        {
                            _inst = new T();
                            var st = _inst as SimpleSingletonProvider<T>;
                            st?.InstanceInit();
                        }
                    }
                }
                var st2 = _inst as SimpleSingletonProvider<T>;
                st2?.OnGetInstance();
                return _inst;
            }
        }

        public static bool hasInstance => _inst != null;

        /// <summary>
        /// 第一次创建实例的调用代码
        /// </summary>
        protected virtual void InstanceInit()
        {
        }

        /// <summary>
        /// 每次获得Instance的调用代码
        /// </summary>
        protected virtual void OnGetInstance()
        {
        }

        /// <summary>
        /// 单例销毁时的调用代码
        /// </summary>
        protected virtual void OnDestroyInstance()
        {
            _inst = null;
        }
    }
}
